// pages/mes/service/service.js
import config from './config';
import Toast from '../../../component/toast/toast';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        items: [],
        mainActiveIndex: 0,
        activeIds: [],
        nickName: '',
        avatarUrl: ''
    },

    onClickNav({
        detail
    }) {
        let activeIds = new Array(this.data.items[detail.index].Child.length);
        this.setData({
            mainActiveIndex: detail.index || 0,
            activeIds: activeIds
        });
    },

    onClickItem({
        detail
    }) {
        let _activeIds = this.data.activeIds;
        this.data.activeIds.splice(detail, 1, !_activeIds[detail]);
        this.setData({
            activeIds: this.data.activeIds
        });
    },
    onStep() {
        let childItem = [];
        let mainIndex = this.data.mainActiveIndex;
        let activeIds = this.data.activeIds;
        for (let i = 0; i < this.data.items[mainIndex].Child.length; i++) {
            if (activeIds[i]) {
                childItem.push(this.data.items[mainIndex].Child[i]);
            }
        }
        if (childItem.length <= 0) {
            Toast("请选择条目！");
            return;
        }
        let id = this.data.items[mainIndex].Id;
        let typeName = this.data.items[mainIndex].TypeName;
        let titleTips = this.data.items[mainIndex].TitleTips;
        let desTips = this.data.items[mainIndex].DesTips;
        app.globalData.serverItemTypes = childItem;
        let obj = {
            id: id,
            typeName: typeName,
            unitList: this.data.items[mainIndex].UnitList,
            nickName: this.data.nickName,
            avatarUrl: this.data.avatarUrl,
            titleTips: titleTips,
            desTips: desTips
        };

        wx.navigateTo({
            url: '/pages/mes/issue-service/issue-service?obj=' + JSON.stringify(obj)
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getMyServiceTypes,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function(res) {
                //   console.log(JSON.stringify(res.data));
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        // console.log(JSON.stringify(res.data.BodyContent));
                        let activeIds = new Array(res.data.BodyContent[that.data.mainActiveIndex].Child.length);
                        that.setData({
                            items: res.data.BodyContent,
                            activeIds: activeIds,
                            nickName: options.nickName,
                            avatarUrl: options.avatarUrl
                        });
                    }
                }
            },
            fail: function(res) {
                console.log(JSON.stringify(res));
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})